home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / startup.el.z / startup.el
Encoding:
Text File  |  1998-05-21  |  52.9 KB  |  1,447 lines

  1. ;;; startup.el --- process XEmacs shell arguments
  2.  
  3. ;; Copyright (C) 1985-1986, 1990, 1992-1997 Free Software Foundation, Inc.
  4. ;; Copyright (c) 1993, 1994 Sun Microsystems, Inc.
  5. ;; Copyright (C) 1995 Board of Trustees, University of Illinois
  6.  
  7. ;; Maintainer: XEmacs
  8. ;; Keywords: internal
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  24. ;; Free Software Foundation, 59 Temple Place - Suite 330,
  25. ;; Boston, MA 02111-1307, USA.
  26.  
  27. ;;; Synched up with: FSF 19.34.
  28.  
  29. ;;; Code:
  30.  
  31. ;;; -batch, -t, and -nw are processed by main() in emacs.c and are 
  32. ;;; never seen by lisp code.
  33.  
  34. ;;; -version and -help are special-cased as well: they imply -batch,
  35. ;;; but are left on the list for lisp code to process.
  36.  
  37.  
  38. (setq top-level '(normal-top-level))
  39.  
  40. (defvar command-line-processed nil "t once command line has been processed")
  41.  
  42. (defconst startup-message-timeout 12000) ; More or less disable the timeout
  43.  
  44. (defconst inhibit-startup-message nil
  45.   "*Non-nil inhibits the initial startup message.
  46. This is for use in your personal init file, once you are familiar
  47. with the contents of the startup message.")
  48.  
  49. ;; #### FSFmacs randomness
  50. ;;(defconst inhibit-startup-echo-area-message nil
  51. ;;  "*Non-nil inhibits the initial startup echo area message.
  52. ;;Inhibition takes effect only if your `.emacs' file contains
  53. ;;a line of this form:
  54. ;; (setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\")
  55. ;;If your `.emacs' file is byte-compiled, use the following form instead:
  56. ;; (eval '(setq inhibit-startup-echo-area-message \"YOUR-USER-NAME\"))
  57. ;;Thus, someone else using a copy of your `.emacs' file will see
  58. ;;the startup message unless he personally acts to inhibit it.")
  59.  
  60. (defconst inhibit-default-init nil
  61.   "*Non-nil inhibits loading the `default' library.")
  62.  
  63. (defvar command-line-args-left nil
  64.   "List of command-line args not yet processed.") ; bound by `command-line'
  65.  
  66. (defvar command-line-default-directory nil
  67.   "Default directory to use for command line arguments.
  68. This is normally copied from `default-directory' when XEmacs starts.")
  69.  
  70. (defvar before-init-hook nil
  71.   "Functions to call after handling urgent options but before init files.
  72. The frame system uses this to open frames to display messages while
  73. XEmacs loads the user's initialization file.")
  74.  
  75. (defvar after-init-hook nil
  76.   "*Functions to call after loading the init file (`~/.emacs').
  77. The call is not protected by a condition-case, so you can set `debug-on-error'
  78. in `.emacs', and put all the actual code on `after-init-hook'.")
  79.  
  80. (defvar term-setup-hook nil
  81.   "*Functions to be called after loading terminal-specific Lisp code.
  82. See `run-hooks'.  This variable exists for users to set, so as to
  83. override the definitions made by the terminal-specific file.  XEmacs
  84. never sets this variable itself.")
  85.  
  86. (defvar keyboard-type nil
  87.   "The brand of keyboard you are using.
  88. This variable is used to define the proper function and keypad keys
  89. for use under X.  It is used in a fashion analogous to the environment
  90. value TERM.")
  91.  
  92. (defvar window-setup-hook nil
  93.   "Normal hook run to initialize window system display.
  94. XEmacs runs this hook after processing the command line arguments and loading
  95. the user's init file.")
  96.  
  97. (defconst initial-major-mode 'lisp-interaction-mode
  98.   "Major mode command symbol to use for the initial *scratch* buffer.")
  99.  
  100. (defvar init-file-user nil
  101.   "Identity of user whose `~/.emacs' file is or was read.
  102. The value is nil if no init file is being used; otherwise, it may be either
  103. the null string, meaning that the init file was taken from the user that
  104. originally logged in, or it may be a string containing a user's name.
  105.  
  106. In either of the latter cases, `(concat \"~\" init-file-user \"/\")'
  107. evaluates to the name of the directory where the `.emacs.el' file was
  108. looked for.
  109.  
  110. Setting `init-file-user' does not prevent Emacs from loading
  111. `site-start.el'.  The only way to do that is to use `--no-site-file'.")
  112.  
  113. ;; #### called `site-run-file' in FSFmacs
  114.  
  115. (defvar site-start-file (purecopy "site-start")
  116.   "File containing site-wide run-time initializations.
  117. This file is loaded at run-time before `~/.xemacs/init.el'.  It
  118. contains inits that need to be in place for the entire site, but
  119. which, due to their higher incidence of change, don't make sense to
  120. load into XEmacs' dumped image.  Thus, the run-time load order is:
  121.  
  122.   1. file described in this variable, if non-nil;
  123.   2. `~/.xemacs/init.el';
  124.   3. `/path/to/xemacs/lisp/default.el'.
  125.  
  126. Don't use the `site-start.el' file for things some users may not like.
  127. Put them in `default.el' instead, so that users can more easily
  128. override them.  Users can prevent loading `default.el' with the `-q'
  129. option or by setting `inhibit-default-init' in their own init files,
  130. but inhibiting `site-start.el' requires `--no-site-file', which
  131. is less convenient.")
  132.  
  133. ;;(defconst iso-8859-1-locale-regexp "8859[-_]?1"
  134. ;;  "Regexp that specifies when to enable the ISO 8859-1 character set.
  135. ;;We do that if this regexp matches the locale name
  136. ;;specified by the LC_ALL, LC_CTYPE and LANG environment variables.")
  137.  
  138. (defvar mail-host-address nil
  139.   "*Name of this machine, for purposes of naming users.")
  140.  
  141. (defvar user-mail-address nil
  142.   "*Full mailing address of this user.
  143. This is initialized based on `mail-host-address',
  144. after your init file is read, in case it sets `mail-host-address'.")
  145.  
  146. (defvar auto-save-list-file-prefix "~/.xemacs/.saves-"
  147.   "Prefix for generating auto-save-list-file-name.
  148. Emacs's pid and the system name will be appended to
  149. this prefix to create a unique file name.")
  150.  
  151. (defvar init-file-debug nil)
  152.  
  153. (defvar init-file-had-error nil)
  154.  
  155. (defvar init-file-loaded nil
  156.   "True after the user's init file has been loaded (or suppressed with -q).
  157. This will be true when `after-init-hook' is run and at all times
  158. after, and will not be true at any time before.")
  159.  
  160. (defvar initial-frame-unmapped-p nil)
  161.  
  162.  
  163.  
  164. (defvar command-switch-alist
  165.   (purecopy
  166.    '(("-help"    . command-line-do-help)
  167.      ("-flags"    . command-line-do-help)
  168.      ("-?"    . command-line-do-help)
  169.      ("-version". command-line-do-version)
  170.      ("-V"    . command-line-do-version)
  171.      ("-funcall". command-line-do-funcall)
  172.      ("-f"    . command-line-do-funcall)
  173.      ("-e"    . command-line-do-funcall-1)
  174.      ("-eval"    . command-line-do-eval)
  175.      ("-load"    . command-line-do-load)
  176.      ("-l"    . command-line-do-load)
  177.      ("-insert"    . command-line-do-insert)
  178.      ("-i"    . command-line-do-insert)
  179.      ("-kill"    . command-line-do-kill)
  180.      ;; Options like +35 are handled specially.
  181.      ;; Window-system, site, or package-specific code might add to this.
  182.      ;; X11 handles its options by letting Xt remove args from this list.
  183.      ))
  184.   "Alist of command-line switches.
  185. Elements look like (SWITCH-STRING . HANDLER-FUNCTION).
  186. HANDLER-FUNCTION receives switch name as sole arg;
  187. remaining command-line args are in the variable `command-line-args-left'.")
  188.  
  189. ;;; default switches
  190. ;;; Note: these doc strings are semi-magical.
  191.  
  192. (defun command-line-do-help (arg)
  193.   "Print the XEmacs usage message and exit."
  194.   (let ((standard-output 'external-debugging-output))
  195.     (princ (concat "\n" (emacs-version) "\n\n"))
  196.     (princ
  197.      (if (featurep 'x)
  198.      (concat (emacs-name)
  199.          " accepts all standard X Toolkit command line options.\n"
  200.          "In addition, the")
  201.        "The"))
  202.     (princ " following options are accepted:
  203.  
  204.   -t <device>           Use TTY <device> instead of the terminal for input
  205.                         and output.  This implies the -nw option.
  206.   -nw                   Inhibit the use of any window-system-specific
  207.                         display code: use the current tty.
  208.   -batch                Execute noninteractively (messages go to stderr).
  209.   -debug-init           Enter the debugger if an error in the init file occurs.
  210.   -unmapped             Do not map the initial frame.
  211.   -no-site-file         Do not load the site-specific init file (site-start.el).
  212.   -no-init-file         Do not load the user-specific init file (~/.emacs).
  213.   -no-packages        Do not process the package path.
  214.   -vanilla        Equivalent to -q -no-site-file -no-packages.
  215.   -q                    Same as -no-init-file.
  216.   -user <user>          Load user's init file instead of your own.
  217.   -u <user>             Same as -user.\n")
  218.    (let ((l command-switch-alist)
  219.       (insert (lambda (&rest x)
  220.             (princ "  ")
  221.             (let ((len 2))
  222.               (while x
  223.             (princ (car x))
  224.             (incf len (length (car x)))
  225.             (setq x (cdr x)))
  226.               (when (>= len 24)
  227.             (terpri) (setq len 0))
  228.               (while (< len 24)
  229.             (princ " ")
  230.             (incf len))))))
  231.       (while l
  232.         (let ((name (car (car l)))
  233.               (fn (cdr (car l)))
  234.           doc arg cons)
  235.       (cond
  236.        ((and (symbolp fn) (get fn 'undocumented)) nil)
  237.        (t
  238.         (setq doc (documentation fn))
  239.         (if (member doc '(nil "")) (setq doc "(undocumented)"))
  240.         (cond ((string-match "\n\\(<.*>\\)\n?\\'" doc)
  241.            ;; Doc of the form "The frobber switch\n<arg1> <arg2>"
  242.            (setq arg (substring doc (match-beginning 1) (match-end 1))
  243.              doc (substring doc 0 (match-beginning 0))))
  244.           ((string-match "\n+\\'" doc)
  245.            (setq doc (substring doc 0 (match-beginning 0)))))
  246.         (if (and (setq cons (rassq fn command-switch-alist))
  247.              (not (eq cons (car l))))
  248.         (setq doc (format "Same as %s." (car cons))))
  249.         (if arg
  250.         (funcall insert name " " arg)
  251.           (funcall insert name))
  252.         (princ doc)
  253.         (terpri))))
  254.         (setq l (cdr l))))
  255.     (princ (concat "\
  256.   +N <file>             Start displaying <file> at line N.
  257.  
  258. Anything else is considered a file name, and is placed into a buffer for
  259. editing.
  260.  
  261. " (emacs-name) " has an online tutorial and manuals.  Type ^Ht (Control-h t) after
  262. starting XEmacs to run the tutorial.  Type ^Hi to enter the manual browser.
  263. Type ^H^H^H (Control-h Control-h Control-h) to get more help options.\n")
  264.  
  265.     (kill-emacs 0))))
  266.  
  267. (defun command-line-do-funcall (arg)
  268.   "Invoke the named lisp function with no arguments.
  269. <function>"
  270.   (funcall (intern (pop command-line-args-left))))
  271. (fset 'command-line-do-funcall-1 'command-line-do-funcall)
  272. (put 'command-line-do-funcall-1 'undocumented t)
  273.  
  274. (defun command-line-do-eval (arg)
  275.   "Evaluate the lisp form.  Quote it carefully.
  276. <form>"
  277.   (eval (read (pop command-line-args-left))))
  278.  
  279. (defun command-line-do-load (arg)
  280.   "Load the named file of Lisp code into XEmacs.
  281. <file>"
  282.   (let ((file (pop command-line-args-left)))
  283.     ;; Take file from default dir if it exists there;
  284.     ;; otherwise let `load' search for it.
  285.     (if (file-exists-p (expand-file-name file))
  286.     (setq file (expand-file-name file)))
  287.     (load file nil t)))
  288.  
  289. (defun command-line-do-insert (arg)
  290.   "Insert file into the current buffer.
  291. <file>"
  292.   (insert-file-contents (pop command-line-args-left)))
  293.  
  294. (defun command-line-do-kill (arg)
  295.   "Exit XEmacs."
  296.   (kill-emacs t))
  297.  
  298. (defun command-line-do-version (arg)
  299.   "Print version info and exit."
  300.   (princ (concat (emacs-version) "\n"))
  301.   (kill-emacs 0))
  302.  
  303.  
  304. ;;; Processing the command line and loading various init files
  305.  
  306. (defun early-error-handler (&rest debugger-args)
  307.   "You should probably not be using this."
  308.   ;; Used as the debugger during XEmacs initialization; if an error occurs,
  309.   ;; print some diagnostics, and kill XEmacs.
  310.  
  311.   ;; output the contents of the warning buffer, since it won't be seen
  312.   ;; otherwise.
  313.   ;; #### kludge!  The call to Feval forces the pending warnings to
  314.   ;; get output.  There definitely needs to be a better way.
  315.   (let ((buffer (eval (get-buffer-create "*Warnings*"))))
  316.     (princ (buffer-substring (point-min buffer) (point-max buffer) buffer)
  317.        'external-debugging-output))
  318.  
  319.   (let ((string "Initialization error")
  320.     (error (nth 1 debugger-args))
  321.     (debug-on-error nil)
  322.     (stream 'external-debugging-output))
  323.     (if (null error)
  324.     (princ string stream)
  325.       (princ (concat "\n" string ": ") stream)
  326.       (condition-case ()
  327.       (display-error error stream)
  328.     (error (princ "<<< error printing error message >>>" stream)))
  329.       (princ "\n" stream)
  330.       (if (memq (car-safe error) '(void-function void-variable))
  331.       (princ "
  332.     This probably means that XEmacs is picking up an old version of
  333.     the lisp library, or that some .elc files are not up-to-date.\n"
  334.          stream)))
  335.     (when (not suppress-early-error-handler-backtrace)
  336.       (let ((print-length 1000)
  337.         (print-level 1000)
  338.         (print-escape-newlines t)
  339.         (print-readably nil))
  340.     (when (getenv "EMACSLOADPATH")
  341.       (princ (format "\n$EMACSLOADPATH is %s" (getenv "EMACSLOADPATH"))
  342.          stream))
  343.     (princ (format "\nexec-directory is %S" exec-directory) stream)
  344.     (princ (format "\ndata-directory is %S" data-directory) stream)
  345.     (princ (format "\ndoc-directory is %S" doc-directory) stream)
  346.     (princ (format "\nload-path is %S" load-path) stream)
  347.     (princ "\n\n" stream)))
  348.     (when (not suppress-early-error-handler-backtrace)
  349.       (backtrace stream t)))
  350.   (kill-emacs -1))
  351.  
  352. (defvar lock-directory)
  353. (defvar superlock-file)
  354.  
  355. (defun normal-top-level ()
  356.   (if command-line-processed
  357.       (message "Back to top level.")
  358.     (setq command-line-processed t)
  359.     ;; Canonicalize HOME (PWD is canonicalized by init_buffer in buffer.c)
  360.     (unless (eq system-type 'vax-vms)
  361.       (let ((value (getenv "HOME")))
  362.     (if (and value
  363.          (< (length value) (length default-directory))
  364.          (equal (file-attributes default-directory)
  365.             (file-attributes value)))
  366.         (setq default-directory (file-name-as-directory value)))))
  367.     (setq default-directory (abbreviate-file-name default-directory))
  368.     (initialize-xemacs-paths)
  369.     (unwind-protect
  370.     (command-line)
  371.       ;; Do this again, in case .emacs defined more abbreviations.
  372.       (setq default-directory (abbreviate-file-name default-directory))
  373.       ;; Specify the file for recording all the auto save files of
  374.       ;; this session.  This is used by recover-session.
  375.       (setq auto-save-list-file-name
  376.         (expand-file-name
  377.          (format "%s%d-%s"
  378.              auto-save-list-file-prefix
  379.              (emacs-pid)
  380.              (system-name))))
  381.       (run-hooks 'emacs-startup-hook)
  382.       (and term-setup-hook
  383.        (run-hooks 'term-setup-hook))
  384.       (setq term-setup-hook nil)
  385.       ;;      ;; Modify the initial frame based on what .emacs puts into
  386.       ;;      ;; ...-frame-alist.
  387.       (frame-notice-user-settings)
  388.       ;;      ;;####FSFmacs junk
  389.       ;;      ;; Now we know the user's default font, so add it to the menu.
  390.       ;;      (if (fboundp 'font-menu-add-default)
  391.       ;;      (font-menu-add-default))
  392.       (when window-setup-hook
  393.     (run-hooks 'window-setup-hook))
  394.       (setq window-setup-hook nil))
  395.     ;;####FSFmacs junk
  396.     ;;      (or menubar-bindings-done
  397.     ;;      (precompute-menubar-bindings))
  398.     ))
  399.  
  400. ;;####FSFmacs junk
  401. ;;; Precompute the keyboard equivalents in the menu bar items.
  402. ;;(defun precompute-menubar-bindings ()
  403. ;;  (if (eq window-system 'x)
  404. ;;      (let ((submap (lookup-key global-map [menu-bar])))
  405. ;;    (while submap
  406. ;;      (and (consp (car submap))
  407. ;;           (symbolp (car (car submap)))
  408. ;;           (stringp (car-safe (cdr (car submap))))
  409. ;;           (keymapp (cdr (cdr (car submap))))
  410. ;;           (x-popup-menu nil (cdr (cdr (car submap)))))
  411. ;;      (setq submap (cdr submap))))))
  412.  
  413. (defun command-line-early (args)
  414.   ;; This processes those switches which need to be processed before
  415.   ;; starting up the window system.
  416.  
  417.   (setq command-line-default-directory default-directory)
  418.  
  419.   ;; See if we should import version-control from the environment variable.
  420.   (let ((vc (getenv "VERSION_CONTROL")))
  421.     (cond ((eq vc nil))            ;don't do anything if not set
  422.       ((or (string= vc "t")
  423.            (string= vc "numbered"))
  424.        (setq version-control t))
  425.       ((or (string= vc "nil")
  426.            (string= vc "existing"))
  427.        (setq version-control nil))
  428.       ((or (string= vc "never")
  429.            (string= vc "simple"))
  430.        (setq version-control 'never))))
  431.  
  432.   ;;####FSFmacs
  433.   ;;  (if (let ((ctype
  434.   ;;         ;; Use the first of these three envvars that has a nonempty value.
  435.   ;;         (or (let ((string (getenv "LC_ALL")))
  436.   ;;           (and (not (equal string "")) string))
  437.   ;;         (let ((string (getenv "LC_CTYPE")))
  438.   ;;           (and (not (equal string "")) string))
  439.   ;;         (let ((string (getenv "LANG")))
  440.   ;;           (and (not (equal string "")) string)))))
  441.   ;;    (and ctype
  442.   ;;         (string-match iso-8859-1-locale-regexp ctype)))
  443.   ;;      (progn 
  444.   ;;    (standard-display-european t)
  445.   ;;    (require 'iso-syntax)))
  446.  
  447.   ;; Figure out which user's init file to load,
  448.   ;; either from the environment or from the options.
  449.   (setq init-file-user (if (noninteractive) nil (user-login-name)))
  450.   ;; If user has not done su, use current $HOME to find .emacs.
  451.   (and init-file-user (string= init-file-user (user-real-login-name))
  452.        (setq init-file-user ""))
  453.  
  454.   ;; Allow (at least) these arguments anywhere in the command line
  455.   (let ((new-args nil)
  456.     (arg      nil))
  457.     (while args
  458.       (setq arg (pop args))
  459.       (cond
  460.        ((or (string= arg "-q")
  461.         (string= arg "-no-init-file"))
  462.     (setq init-file-user nil))
  463.        ((string= arg "-no-site-file")
  464.     (setq site-start-file nil))
  465.        ((or (string= arg "-no-packages")
  466.         (string= arg "--no-packages"))
  467.     (setq inhibit-package-init t))
  468.        ((or (string= arg "-vanilla")
  469.         (string= arg "--vanilla"))
  470.     (setq init-file-user nil
  471.           site-start-file nil
  472.           inhibit-package-init t))
  473.        ((or (string= arg "-u")
  474.         (string= arg "-user"))
  475.     (setq init-file-user (pop args)))
  476.        ((string= arg "-debug-init")
  477.     (setq init-file-debug t))
  478.        ((string= arg "-unmapped")
  479.     (setq initial-frame-unmapped-p t))
  480.        ((or (string= arg "--") (string= arg "-"))
  481.     (while args
  482.       (push (pop args) new-args)))
  483.        (t (push arg new-args))))
  484.     
  485.     (nreverse new-args)))
  486.  
  487. (defconst initial-scratch-message "\
  488. ;; This buffer is for notes you don't want to save, and for Lisp evaluation.
  489. ;; If you want to create a file, first visit that file with C-x C-f,
  490. ;; then enter the text in that file's own buffer.
  491.  
  492. "
  493.   "Initial message displayed in *scratch* buffer at startup.
  494. If this is nil, no message will be displayed.")
  495.  
  496. (defun command-line ()
  497.   (let ((command-line-args-left (cdr command-line-args)))
  498.  
  499.     (let ((debugger 'early-error-handler)
  500.       (debug-on-error t))
  501.       (set-default-load-path)
  502.  
  503.       ;; Process magic command-line switches like -q and -u.  Do this
  504.       ;; before creating the first frame because some of these switches
  505.       ;; may affect that.  I think it's ok to do this before establishing
  506.       ;; the X connection, and maybe someday things like -nw can be
  507.       ;; handled here instead of down in C.
  508.       (setq command-line-args-left (command-line-early command-line-args-left))
  509.  
  510.       ;; Setup the toolbar icon directory
  511.       (when (featurep 'toolbar)
  512.     (init-toolbar-location))
  513.  
  514.       ;; Run the window system's init function.  tty is considered to be
  515.       ;; a type of window system for this purpose.  This creates the
  516.       ;; initial (non stdio) device.
  517.       (when (and initial-window-system (not noninteractive))
  518.     (funcall (intern (concat "init-"
  519.                  (symbol-name initial-window-system)
  520.                  "-win"))))
  521.  
  522.       ;; When not in batch mode, this creates the first visible frame,
  523.       ;; and deletes the stdio device.
  524.       (frame-initialize))
  525.  
  526.     ;;
  527.     ;; We have normality, I repeat, we have normality.  Anything you still
  528.     ;; can't cope with is therefore your own problem.  (And we don't need
  529.     ;; to kill XEmacs for it.)
  530.     ;;
  531.  
  532.     ;;; Load init files.
  533.     (load-init-file)
  534.     
  535.     (with-current-buffer (get-buffer "*scratch*")
  536.       (erase-buffer)
  537.       ;; (insert initial-scratch-message)
  538.       (set-buffer-modified-p nil)
  539.       (when (eq major-mode 'fundamental-mode)
  540.     (funcall initial-major-mode)))
  541.  
  542.     ;; Load library for our terminal type.
  543.     ;; User init file can set term-file-prefix to nil to prevent this.
  544.     ;; Note that for any TTY's opened subsequently, the TTY init
  545.     ;; code will run this.
  546.     (when (and (eq 'tty (console-type))
  547.            (not (noninteractive)))
  548.       (load-terminal-library))
  549.  
  550.     ;; Process the remaining args.
  551.     (command-line-1)
  552.  
  553.     ;; it was turned on by default so that the warnings don't get displayed
  554.     ;; until after the splash screen.
  555.     (setq inhibit-warning-display nil)
  556.     ;; If -batch, terminate after processing the command options.
  557.     (when (noninteractive) (kill-emacs t))))
  558.  
  559. (defun load-terminal-library ()          
  560.   (when term-file-prefix
  561.     (let ((term (getenv "TERM"))
  562.       hyphend)
  563.       (while (and term
  564.           (not (load (concat term-file-prefix term) t t)))
  565.     ;; Strip off last hyphen and what follows, then try again
  566.     (if (setq hyphend (string-match "[-_][^-_]+\\'" term))
  567.         (setq term (substring term 0 hyphend))
  568.       (setq term nil))))))
  569.  
  570. (defconst user-init-directory "/"
  571.   "Directory where user initialization and user-installed packages may go.
  572. Note that `~' is automatically prepended to this whereever it is used.")
  573. (define-obsolete-variable-alias
  574.   'emacs-user-extension-dir
  575.   'user-init-directory)
  576.  
  577. (defun load-user-init-file (init-file-user)
  578.   "This function actually reads the init files.
  579. In XEmacs 20.3 this function only looks at .emacs.  In XEmacs 20.4 this
  580. function will first try .xemacs/init, then try .emacs, but only load one
  581. of the two."
  582.   (when init-file-user
  583.     (setq user-init-file
  584.       (cond
  585.        ((eq system-type 'ms-dos)
  586.         (concat "~" init-file-user "/_emacs"))
  587.        (t
  588.         (concat "~" init-file-user "/.emacs"))))
  589.     (load user-init-file t t t)
  590. ;;; This is split out in XEmacs 20.4
  591. ;    (let ((default-custom-file (concat "~"
  592. ;                       init-file-user
  593. ;                       user-init-directory
  594. ;                       "options.el")))
  595. ;      (when (string= custom-file default-custom-file)
  596. ;    (load default-custom-file t t)))
  597.     (unless inhibit-default-init
  598.       (let ((inhibit-startup-message nil))
  599.     ;; Users are supposed to be told their rights.
  600.     ;; (Plus how to get help and how to undo.)
  601.     ;; Don't you dare turn this off for anyone except yourself.
  602.     (load "default" t t)))))
  603.  
  604. ;;; Load user's init file and default ones.
  605. (defun load-init-file ()
  606.   (run-hooks 'before-init-hook)
  607.  
  608.   ;; Run the site-start library if it exists.  The point of this file is
  609.   ;; that it is run before .emacs.  There is no point in doing this after
  610.   ;; .emacs; that is useless.
  611.   (when site-start-file
  612.     (load site-start-file t t))
  613.  
  614.   ;; Sites should not disable this.  Only individuals should disable
  615.   ;; the startup message.
  616.   (setq inhibit-startup-message nil)
  617.  
  618.   (let (debug-on-error-from-init-file
  619.     debug-on-error-should-be-set
  620.     (debug-on-error-initial
  621.      (if (eq init-file-debug t) 'startup init-file-debug)))
  622.     (let ((debug-on-error debug-on-error-initial))
  623.       (if init-file-debug
  624.       ;; Do this without a condition-case if the user wants to debug.
  625.       (load-user-init-file init-file-user)
  626.     (condition-case error
  627.         (progn
  628.           (load-user-init-file init-file-user)
  629.           (setq init-file-had-error nil))
  630.           (error
  631.            (message "Error in init file: ")
  632.            (display-error error nil)
  633.        (setq init-file-had-error t))))
  634.       ;; If we can tell that the init file altered debug-on-error,
  635.       ;; arrange to preserve the value that it set up.
  636.       (or (eq debug-on-error debug-on-error-initial)
  637.       (setq debug-on-error-should-be-set t
  638.         debug-on-error-from-init-file debug-on-error)))
  639.     (when debug-on-error-should-be-set
  640.       (setq debug-on-error debug-on-error-from-init-file)))
  641.  
  642.   (setq init-file-loaded t)
  643.  
  644.   ;; Do this here in case the init file sets mail-host-address.
  645.   ;; Don't do this here unless noninteractive, it is frequently wrong. -sb
  646.   ;; (or user-mail-address
  647.   (when noninteractive
  648.     (setq user-mail-address (concat (user-login-name) "@"
  649.                     (or mail-host-address
  650.                     (system-name)))))
  651.  
  652.   (run-hooks 'after-init-hook)
  653.   nil)
  654.  
  655. (defun load-options-file (filename)
  656.   "Load the file of saved options (from the Options menu) called FILENAME.
  657. Currently this does nothing but call `load', but it might be redefined
  658. in the future to support automatically converting older options files to
  659. a new format, when variables have changed, etc."
  660.   (load filename))
  661.  
  662. (defun command-line-1 ()
  663.   (cond
  664.    ((null command-line-args-left)
  665.     (unless noninteractive
  666.       ;; If there are no switches to process, run the term-setup-hook
  667.       ;; before displaying the copyright notice; there may be some need
  668.       ;; to do it before doing any output.  If we're not going to
  669.       ;; display a copyright notice (because other options are present)
  670.       ;; then this is run after those options are processed.
  671.       (run-hooks 'term-setup-hook)
  672.       ;; Don't let the hook be run twice.
  673.       (setq term-setup-hook nil)
  674.  
  675.       ;; Don't clobber a non-scratch buffer if init file
  676.       ;; has selected it.
  677.       (when (string= (buffer-name) "*scratch*")
  678.     (unless (or inhibit-startup-message
  679.             (input-pending-p))
  680.       (let ((timeout nil))
  681.         (unwind-protect
  682.         ;; Guts of with-timeout
  683.         (catch 'timeout
  684.           (setq timeout (add-timeout startup-message-timeout
  685.                          (lambda (ignore)
  686.                            (condition-case nil
  687.                            (throw 'timeout t)
  688.                          (error nil)))
  689.                          nil))
  690.           (startup-splash-frame)
  691.           (or nil;; (pos-visible-in-window-p (point-min))
  692.               (goto-char (point-min)))
  693.           (sit-for 0)
  694.           (setq unread-command-event (next-command-event)))
  695.           (when timeout (disable-timeout timeout)))))
  696.     (with-current-buffer (get-buffer "*scratch*")
  697.       ;; In case the XEmacs server has already selected
  698.       ;; another buffer, erase the one our message is in.
  699.       (erase-buffer)
  700.       (when (stringp initial-scratch-message)
  701.         (insert initial-scratch-message))
  702.       (set-buffer-modified-p nil)))))
  703.  
  704.    (t
  705.     ;; Command-line-options exist
  706.     (let ((dir command-line-default-directory)
  707.       (file-count 0)
  708.       (line nil)
  709.       (end-of-options nil)
  710.       first-file-buffer file-p arg tem)
  711.       (while command-line-args-left
  712.     (setq arg (pop command-line-args-left))
  713.     (cond
  714.      (end-of-options
  715.       (setq file-p t))
  716.      ((setq tem (when (eq (aref arg 0) ?-)
  717.               (or (assoc arg command-switch-alist)
  718.               (assoc (substring arg 1)
  719.                  command-switch-alist))))
  720.       (funcall (cdr tem) arg))
  721.      ((string-match "\\`\\+[0-9]+\\'" arg)
  722.       (setq line (string-to-int arg)))
  723.      ;; "- file" means don't treat "file" as a switch
  724.      ;;  ("+0 file" has the same effect; "-" added
  725.      ;;   for unixoidiality).
  726.      ;; This is worthless; the `unixoid' way is "./file". -jwz
  727.      ((or (string= arg "-") (string= arg "--"))
  728.       (setq end-of-options t))
  729.      (t
  730.       (setq file-p t)))
  731.     
  732.     (when file-p
  733.       (setq file-p nil)
  734.       (incf file-count)
  735.       (setq arg (expand-file-name arg dir))
  736.       (cond
  737.        ((= file-count 1) (setq first-file-buffer
  738.                    (progn (find-file arg) (current-buffer))))
  739.        (noninteractive (find-file arg))
  740.        (t (find-file-other-window arg)))
  741.       (when line
  742.         (goto-line line)
  743.         (setq line nil))))
  744.       ;; If 3 or more files visited, and not all visible,
  745.       ;; show user what they all are.
  746.       (when (and (not noninteractive)
  747.          (> file-count 2)
  748.          (not (get-buffer-window first-file-buffer)))
  749.     (other-window 1)
  750.     (buffer-menu nil))))))
  751.  
  752. (defvar startup-presentation-hack-keymap
  753.   (let ((map (make-sparse-keymap)))
  754.     (set-keymap-name map 'startup-presentation-hack-keymap)
  755.     (define-key map '[button1] 'startup-presentation-hack)
  756.     (define-key map '[button2] 'startup-presentation-hack)
  757.     map)
  758.   "Putting yesterday in the future tomorrow.")
  759.  
  760. (defun startup-presentation-hack ()
  761.   (interactive)
  762.   (let ((e last-command-event))
  763.     (and (button-press-event-p e)
  764.          (setq e (extent-at (event-point e)
  765.                             (event-buffer e)
  766.                             'startup-presentation-hack))
  767.          (setq e (extent-property e 'startup-presentation-hack))
  768.          (if (consp e)
  769.              (apply (car e) (cdr e))
  770.        (while (keymapp (indirect-function e))
  771.          (let ((map e)
  772.            (overriding-local-map (indirect-function e)))
  773.            (setq e (read-key-sequence
  774.             (let ((p (keymap-prompt map t)))
  775.               (cond ((symbolp map)
  776.                  (if p 
  777.                      (format "%s %s " map p)
  778.                    (format "%s " map)))
  779.                 (p)
  780.                 (t
  781.                  (prin1-to-string map))))))
  782.            (if (and (button-release-event-p (elt e 0))
  783.             (null (key-binding e)))
  784.            (setq e map)        ; try again
  785.          (setq e (key-binding e)))))
  786.        (call-interactively e)))))
  787.  
  788. (defun startup-presentation-hack-help (e)
  789.   (setq e (extent-property e 'startup-presentation-hack))
  790.   (if (consp e)
  791.       (format "Evaluate %S" e)
  792.     (symbol-name e)))
  793.  
  794. (defun splash-frame-present-hack (e v)
  795.   ;;  (set-extent-property e 'mouse-face 'highlight)
  796.   ;;  (set-extent-property e 'keymap
  797.   ;;                       startup-presentation-hack-keymap)
  798.   ;;  (set-extent-property e 'startup-presentation-hack v)
  799.   ;;  (set-extent-property e 'help-echo
  800.   ;;                       'startup-presentation-hack-help))
  801.   )
  802.  
  803. (defun splash-hack-version-string ()
  804.   (save-excursion
  805.     (save-restriction
  806.       (goto-char (point-min))
  807.       (re-search-forward "^XEmacs" nil t)
  808.       (narrow-to-region (point-at-bol) (point-at-eol))
  809.       (goto-char (point-min))
  810.       (when (re-search-forward " \\[Lucid\\]" nil t)
  811.     (delete-region (match-beginning 0) (match-end 0)))
  812.       (when (re-search-forward "[^(][^)]*-[^)]*-" nil t)
  813.     (delete-region (1+ (match-beginning 0)) (match-end 0))
  814.     (insert "("))
  815.       (goto-char (point-max))
  816.       (search-backward " " nil t)
  817.       (when (search-forward "." nil t)
  818.     (delete-region (1- (point)) (point-max))))))
  819.  
  820. (defun splash-frame-present (l)
  821.   (cond ((stringp l)
  822.          (insert l))
  823.         ((eq (car-safe l) 'face)
  824.          ;; (face name string)
  825.          (let ((p (point)))
  826.            (splash-frame-present (elt l 2))
  827.            (if (fboundp 'set-extent-face)
  828.                (set-extent-face (make-extent p (point))
  829.                                 (elt l 1)))))
  830.         ((eq (car-safe l) 'key)
  831.          (let* ((c (elt l 1))
  832.                 (p (point))
  833.                 (k (where-is-internal c nil t)))
  834.            (insert (if k (key-description k)
  835.              (format "M-x %s" c)))
  836.            (if (fboundp 'set-extent-face)
  837.                (let ((e (make-extent p (point))))
  838.                  (set-extent-face e 'bold)
  839.                  (splash-frame-present-hack e c)))))
  840.         ((eq (car-safe l) 'funcall)
  841.          ;; (funcall (fun . args) string)
  842.          (let ((p (point)))
  843.            (splash-frame-present (elt l 2))
  844.            (if (fboundp 'set-extent-face)
  845.                (splash-frame-present-hack (make-extent p (point))
  846.                       (elt l 1)))))
  847.     ((consp l)
  848.      (mapcar 'splash-frame-present l))
  849.         (t
  850.          (error "WTF!?"))))
  851.  
  852. (defun startup-center-spaces (glyph)
  853.   ;; Return the number of spaces to insert in order to center
  854.   ;; the given glyph (may be a string or a pixmap).
  855.   ;; Assume spaces are as wide as avg-pixwidth.  
  856.   ;; Won't be quite right for proportional fonts, but it's the best we can do.
  857.   ;; Maybe the new redisplay will export something a glyph-width function.
  858.   ;;; #### Yes, there is a glyph-width function but it isn't quite what
  859.   ;;; #### this was expecting.  Or is it?
  860.   ;; (An alternate way to get avg-pixwidth would be to use x-font-properties
  861.   ;; and calculate RESOLUTION_X * AVERAGE_WIDTH / 722.7, but it's no better.)
  862.  
  863.   ;; This function is used in about.el too.
  864.   (let* ((avg-pixwidth     (round (/ (frame-pixel-width) (frame-width))))
  865.      (fill-area-width  (* avg-pixwidth (- fill-column left-margin)))
  866.      (glyph-pixwidth   (cond ((stringp glyph) 
  867.                   (* avg-pixwidth (length glyph)))
  868.                  ;; #### the pixmap option should be removed
  869.                  ;;((pixmapp glyph)
  870.                  ;; (pixmap-width glyph))
  871.                  ((glyphp glyph)
  872.                   (glyph-width glyph))
  873.                  (t
  874.                   (error "startup-center-spaces: bad arg")))))
  875.     (+ left-margin
  876.        (round (/ (/ (- fill-area-width glyph-pixwidth) 2) avg-pixwidth)))))
  877.  
  878. (defun startup-splash-frame-body ()
  879.   `("\n" ,(emacs-version) "\n"
  880.     ,@(if (string-match "beta" emacs-version)
  881.       `( (face (bold blue) ( "This is an Experimental version of XEmacs. "
  882.                  " Type " (key describe-beta)
  883.                  " to see what this means.\n")))
  884.     `( "\n"))
  885.     (face bold-italic "\
  886. Copyright (C) 1985-1997 Free Software Foundation, Inc.
  887. Copyright (C) 1990-1994 Lucid, Inc.
  888. Copyright (C) 1993-1997 Sun Microsystems, Inc. All Rights Reserved.
  889. Copyright (C) 1994-1996 Board of Trustees, University of Illinois
  890. Copyright (C) 1995-1996 Ben Wing\n\n")
  891.     
  892.     ,@(if (featurep 'sparcworks)
  893.           `( "\
  894. Sun provides support for the WorkShop/XEmacs integration package only.
  895. All other XEmacs packages are provided to you \"AS IS\".
  896. For full details, type " (key describe-no-warranty)
  897. " to refer to the GPL Version 2, dated June 1991.\n\n"
  898. ,@(let ((lang (or (getenv "LC_ALL") (getenv "LC_MESSAGES") (getenv "LANG"))))
  899.     (if (and
  900.          (not (featurep 'mule))         ; Already got mule?
  901.          (not (eq 'tty (console-type))) ; No Mule support on tty's yet
  902.          lang                           ; Non-English locale?
  903.          (not (string= lang "C"))
  904.          (not (string-match "^en" lang))
  905.          (locate-file "xemacs-mule" exec-path)) ; Comes with Sun WorkShop
  906.         '( "\
  907. This version of XEmacs has been built with support for Latin-1 languages only.
  908. To handle other languages you need to run a Multi-lingual (`Mule') version of
  909. XEmacs, by either running the command `xemacs-mule', or by using the X resource
  910. `ESERVE*defaultXEmacsPath: xemacs-mule' when starting XEmacs from Sun WorkShop.\n\n"))))
  911.  
  912.         '("XEmacs comes with ABSOLUTELY NO WARRANTY; type "
  913.           (key describe-no-warranty) " for full details.\n"))
  914.     
  915.     "You may give out copies of XEmacs; type "
  916.     (key describe-copying) " to see the conditions.\n"
  917.     "Type " (key describe-distribution)
  918.     " for information on getting the latest version.\n\n"
  919.  
  920.     "Type " (key help-command) " or use the " (face bold "Help") " menu to get help.\n"
  921.     "Type " (key advertised-undo) " to undo changes  (`C-' means use the Control key).\n"
  922.     "To get out of XEmacs, type " (key save-buffers-kill-emacs) ".\n"
  923.     "Type " (key help-with-tutorial) " for a tutorial on using XEmacs.\n"
  924.     "Type " (key info) " to enter Info, "
  925.     "which you can use to read online documentation.\n"
  926.     (face (bold red) ( "\
  927. For tips and answers to frequently asked questions, see the XEmacs FAQ.
  928. \(It's on the Help menu, or type " (key xemacs-local-faq) " [a capital F!].\)"))))
  929.  
  930. (defun startup-splash-frame ()
  931.   (let ((p (point))
  932.         (cramped-p (eq 'tty (console-type))))
  933.     (unless cramped-p (insert "\n"))
  934.     (indent-to (startup-center-spaces xemacs-logo))
  935.     (set-extent-begin-glyph (make-extent (point) (point)) xemacs-logo)
  936.     (insert (if cramped-p "\n" "\n\n"))
  937.     (splash-frame-present-hack (make-extent p (point)) 'about-xemacs))
  938.  
  939.   (let ((after-change-functions nil))    ; no font-lock, thank you
  940.     (dolist (l (startup-splash-frame-body))
  941.       (splash-frame-present l)))
  942.   (splash-hack-version-string)
  943.   (set-buffer-modified-p nil))
  944.  
  945. ;;  (let ((present-file
  946. ;;         #'(lambda (f)
  947. ;;             (splash-frame-present
  948. ;;          (list 'funcall
  949. ;;            (list 'find-file-other-window
  950. ;;              (expand-file-name f data-directory))
  951. ;;            f)))))
  952. ;;    (insert "For customization examples, see the files ")
  953. ;;    (funcall present-file "sample.emacs")
  954. ;;    (insert " and ")
  955. ;;    (funcall present-file "sample.Xdefaults")
  956. ;;    (insert (format "\nin the directory %s." data-directory)))
  957.  
  958.  
  959. ;;;; Computing the default load-path, etc.
  960. ;;;
  961. ;;; This stuff is a complete mess and isn't nearly as general as it 
  962. ;;; thinks it is.  It should be rethunk.  In particular, too much logic
  963. ;;; is duplicated between the code that looks around for the various
  964. ;;; directories, and the code which suggests where to create the various
  965. ;;; directories once it decides they are missing.
  966.  
  967. ;;; The source directory has this layout:
  968. ;;;
  969. ;;;    BUILD_ROOT/src/xemacs*              argv[0]
  970. ;;;    BUILD_ROOT/xemacs*              argv[0], possibly
  971. ;;;    BUILD_ROOT/lisp/
  972. ;;;    BUILD_ROOT/etc/                  data-directory
  973. ;;;    BUILD_ROOT/info/
  974. ;;;    BUILD_ROOT/lib-src/              exec-directory, doc-directory
  975. ;;;    BUILD_ROOT/lock/
  976. ;;;
  977. ;;; The default tree created by "make install" has this layout:
  978. ;;;
  979. ;;;    PREFIX/bin/xemacs*              argv[0]
  980. ;;;    PREFIX/lib/xemacs-VERSION/lisp/
  981. ;;;    PREFIX/lib/xemacs-VERSION/etc/          data-directory
  982. ;;;    PREFIX/lib/xemacs-VERSION/info/
  983. ;;;    PREFIX/lib/xemacs-VERSION/CONFIGURATION/      exec-directory, doc-directory
  984. ;;;    PREFIX/lib/xemacs/lock/
  985. ;;;    PREFIX/lib/xemacs/site-lisp/
  986. ;;;
  987. ;;; The binary packages we ship have that layout, except that argv[0] has
  988. ;;; been moved one level deeper under the bin directory:
  989. ;;;
  990. ;;;    PREFIX/bin/CONFIGURATION/xemacs*
  991. ;;;
  992. ;;; The following code has to deal with at least the above three situations,
  993. ;;; and it should be possible for it to deal with more.  Though perhaps that
  994. ;;; does cover it all?  The trick is, when something is missing, realizing
  995. ;;; which of those three layouts is mostly in place, so that we can suggest
  996. ;;; the right directories in the error message.
  997.  
  998.  
  999. ;; extremely low-tech debugging, since this happens so early in startup.
  1000. ;;(or (fboundp 'orig-file-directory-p)
  1001. ;;    (fset 'orig-file-directory-p (symbol-function 'file-directory-p)))
  1002. ;;(defun file-directory-p (path)
  1003. ;;  (send-string-to-terminal (format "PROBING %S" path))
  1004. ;;  (let ((v (orig-file-directory-p path)))
  1005. ;;    (send-string-to-terminal (format " -> %S\n" v))
  1006. ;;    v))
  1007.  
  1008. (defun startup-make-version-dir ()
  1009.   (let ((version (and (string-match "\\`[^0-9]*\\([0-9]+\\.[0-9]+\\)"
  1010.                     emacs-version)
  1011.               (substring emacs-version
  1012.                  (match-beginning 1) (match-end 1)))))
  1013.     (if (string-match "(beta *\\([0-9]+\\))" emacs-version)
  1014.     (setq version (concat version "-b"
  1015.                   (substring emacs-version (match-beginning 1)
  1016.                      (match-end 1)))))
  1017.     (if (string-match "(alpha *\\([0-9]+\\))" emacs-version)
  1018.     (setq version (concat version "-a"
  1019.                   (substring emacs-version (match-beginning 1)
  1020.                      (match-end 1)))))
  1021.     (concat "lib/xemacs-" version)))
  1022.  
  1023. (defun find-emacs-root-internal-1 (path lisp-p)
  1024.   ;; (prin1 (format "f-e-r-i-1:  %s\n" path))
  1025.   (let ((dir (file-name-directory path)))
  1026.     (or
  1027.      ;;
  1028.      ;; If this directory is a plausible root of the XEmacs tree, return it.
  1029.      ;;
  1030.      (and (or (not lisp-p)
  1031.           (file-directory-p (expand-file-name "lisp/prim" dir)))
  1032.       (or (file-directory-p (expand-file-name "lib-src" dir))
  1033.           (file-directory-p (expand-file-name system-configuration dir)))
  1034.       dir)
  1035.      ;;
  1036.      ;; If the parent of this directory is a plausible root, use it.
  1037.      ;; (But don't do so recursively!)
  1038.      ;;
  1039.      (and (or (not lisp-p)
  1040.           (file-directory-p (expand-file-name "../lisp/prim" dir)))
  1041.       (or (file-directory-p (expand-file-name
  1042.                  (format "../%s" system-configuration)
  1043.                  dir))
  1044.           (file-directory-p (expand-file-name "../lib-src" dir)))
  1045.       (expand-file-name "../" dir))
  1046.  
  1047.      ;; 
  1048.      ;; (--run-in-place) Same thing, but from one directory level deeper.
  1049.      ;;
  1050.      (and (or (not lisp-p)
  1051.           (file-directory-p (expand-file-name "../../lisp/prim" dir)))
  1052.       (or (file-directory-p (expand-file-name
  1053.                  (format "../%s" system-configuration)
  1054.                  dir))
  1055.           (file-directory-p 
  1056.            (expand-file-name 
  1057.         (format "../../lib-src/%s" system-configuration) dir)))
  1058.       (expand-file-name "../.." dir))
  1059.  
  1060.      ;; If ../lib/xemacs-<version> exists check it.
  1061.      ;; This is of the form "xemacs-19.10/" or "xemacs-19.10-b7/".
  1062.      ;;
  1063.      (let ((ver-dir (concat "../" (startup-make-version-dir))))
  1064.        (and (or (not lisp-p)
  1065.         (file-directory-p (expand-file-name
  1066.                    (format "%s/lisp/prim" ver-dir)
  1067.                    dir)))
  1068.         (or (file-directory-p (expand-file-name
  1069.                    (format "%s/%s" ver-dir
  1070.                        system-configuration)
  1071.                    dir))
  1072.         (file-directory-p (expand-file-name
  1073.                    (format "%s/lib-src" ver-dir)
  1074.                    dir)))
  1075.         (expand-file-name (file-name-as-directory ver-dir) dir)))
  1076.      ;;
  1077.      ;; Same thing, but one higher: ../../lib/xemacs-<version>.
  1078.      ;;
  1079.      (let ((ver-dir (concat "../../" (startup-make-version-dir))))
  1080.        (and (or (not lisp-p)
  1081.         (file-directory-p (expand-file-name
  1082.                    (format "%s/lisp/prim" ver-dir)
  1083.                    dir)))
  1084.         (or (file-directory-p (expand-file-name
  1085.                    (format "%s/%s" ver-dir
  1086.                        system-configuration)
  1087.                    dir))
  1088.         (file-directory-p (expand-file-name
  1089.                    (format "%s/lib-src" ver-dir)
  1090.                    dir)))
  1091.         (expand-file-name (file-name-as-directory ver-dir) dir)))
  1092.      ;;
  1093.      ;; If that doesn't work, and the XEmacs executable is a symlink, then
  1094.      ;; chase the link and try again there.
  1095.      ;;
  1096.      (and (setq path (file-symlink-p path))
  1097.       (find-emacs-root-internal-1 (expand-file-name path dir) lisp-p))
  1098.      ;;
  1099.      ;; Otherwise, this directory just doesn't cut it.
  1100.      ;; Some bozos think they can use the 18.59 lisp directory with 19.*.
  1101.      ;; This is because they're not using their brains.  But it might be
  1102.      ;; nice to notice that that is happening and point them in the
  1103.      ;; general direction of a clue.
  1104.      ;;
  1105.      nil)))
  1106.  
  1107. (defun find-emacs-root-internal (path)
  1108.   ;;  (send-string-to-terminal (format "FINDING ROOT FOR %S\n" path))
  1109.   ;; first look for lisp/prim and lib-src; then just look for lib-src.
  1110.   ;; XEmacs can run (kind of) if the lisp directory is omitted, which
  1111.   ;; some people might want to do for space reasons.
  1112.   (or (find-emacs-root-internal-1 path t)
  1113.       ;; (find-emacs-root-internal-1 path nil)
  1114.       ;; If we don't succeed we are going to crash and burn for sure.
  1115.       ;; Try some paths relative to prefix-directory if it isn't nil.
  1116.       ;; This is definitely necessary in cases such as when we're used
  1117.       ;; as a login shell since we can't determine the invocation
  1118.       ;; directory in that case.
  1119.  
  1120.       (find-emacs-root-internal-1
  1121.        (format "%s/bin/%s" prefix-directory invocation-name) t)
  1122.       (find-emacs-root-internal-1
  1123.        (format "%s/bin/%s" prefix-directory invocation-name) nil)
  1124.       (find-emacs-root-internal-1
  1125.        (format "%s/lib/%s" prefix-directory invocation-name) t)
  1126.       (find-emacs-root-internal-1
  1127.        (format "%s/lib/%s" prefix-directory invocation-name) nil)
  1128.  
  1129.       ;; We're desperate -- try the prefix-directory correctly.
  1130.       (find-emacs-root-internal-1
  1131.        (format "%s/%s/foo" prefix-directory (startup-make-version-dir)) t)
  1132.       (find-emacs-root-internal-1
  1133.        (format "%s/%s/foo" prefix-directory (startup-make-version-dir)) nil)
  1134.       ))
  1135.  
  1136. (defun set-default-load-path ()
  1137.   ;; XEmacs -- Steven Baur says invocation directory is nil if you
  1138.   ;; try to use XEmacs as a login shell.
  1139.   (or invocation-directory (setq invocation-directory default-directory))
  1140.   (setq invocation-directory
  1141.     ;; don't let /tmp_mnt/... get into the load-path or exec-path.
  1142.     (abbreviate-file-name invocation-directory))
  1143.  
  1144.   ;; #### FSFmacs recognizes environment vars EMACSLOCKDIR, etc.
  1145.   (let* ((root (find-emacs-root-internal (concat invocation-directory
  1146.                          invocation-name)))
  1147.      (lisp (and root
  1148.             (let ((f (expand-file-name "lisp" root)))
  1149.               (and (file-directory-p f) f))))
  1150.      (site-lisp
  1151.       (and root
  1152.            (or
  1153.         (let ((f (expand-file-name "xemacs/site-lisp" root)))
  1154.           (and (file-directory-p f) f))
  1155.         (let ((f (expand-file-name "../xemacs/site-lisp" root)))
  1156.           (and (file-directory-p f) f))
  1157.         ;; the next two are for --run-in-place
  1158.         (let ((f (expand-file-name "site-lisp" root)))
  1159.           (and (file-directory-p f) f))
  1160.         (let ((f (expand-file-name "lisp/site-lisp" root)))
  1161.           (and (file-directory-p f) f))
  1162.         )))
  1163.      (lib-src
  1164.       (and root
  1165.            (or
  1166.         (let ((f (expand-file-name
  1167.               (concat "lib-src/" system-configuration)
  1168.               root)))
  1169.           (and (file-directory-p f) f))
  1170.         (let ((f (expand-file-name "lib-src" root)))
  1171.           (and (file-directory-p f) f))
  1172.         (let ((f (expand-file-name system-configuration root)))
  1173.           (and (file-directory-p f) f)))))
  1174.      (etc
  1175.       (and root
  1176.            (let ((f (expand-file-name "etc" root)))
  1177.          (and (file-directory-p f) f))))
  1178.      (info
  1179.       (and root
  1180.            (let ((f (expand-file-name "info" root)))
  1181.          (and (file-directory-p f) (file-name-as-directory f)))))
  1182.      (packages
  1183.       (and root
  1184.            (let ((f (expand-file-name "packages" root)))
  1185.          (and (file-directory-p f) (file-name-as-directory f)))))
  1186.      (lock
  1187.       (and root
  1188.            (boundp 'lock-directory)
  1189.            (if (and lock-directory (file-directory-p lock-directory))
  1190.            (file-name-as-directory lock-directory)
  1191.          (or
  1192.           (let ((f (expand-file-name "xemacs/lock" root)))
  1193.             (and (file-directory-p f)
  1194.              (file-name-as-directory f)))
  1195.           (let ((f (expand-file-name "../xemacs/lock" root)))
  1196.             (and (file-directory-p f)
  1197.              (file-name-as-directory f)))
  1198.           (let ((f (expand-file-name "lock" root)))
  1199.             (and (file-directory-p f)
  1200.              (file-name-as-directory f)))
  1201.           ;; if none of them exist, make the "guess" be
  1202.           ;; the one that set-default-load-path-warning
  1203.           ;; will suggest.
  1204.           (file-name-as-directory
  1205.            (expand-file-name "../xemacs/lock" root))
  1206.           )))))
  1207.     
  1208.     ;; 1996/12/6 by MORIOKA Tomohiko <morioka@jaist.ac.jp>
  1209.     ;;    define `default-load-path' for file-detect.el
  1210.     (setq default-load-path load-path)
  1211.  
  1212.     ;; add site-lisp dir to load-path
  1213.     (when site-lisp
  1214.       ;; If the site-lisp dir isn't on the load-path, add it to the end.
  1215.       (or (member site-lisp load-path)
  1216.       (setq load-path (append load-path (list (concat site-lisp "/")))))
  1217.       ;; Also add any direct subdirectories of the site-lisp directory
  1218.       ;; to the load-path.  But don't add dirs whose names begin
  1219.       ;; with dot or hyphen.
  1220.       (let ((files (directory-files site-lisp nil "^[^-.]" nil 'dirs-only))
  1221.         file)
  1222.     (while files
  1223.       (setq file (car files))
  1224.       (if (and (not (member file '("RCS" "CVS" "SCCS")))
  1225.            (setq file (expand-file-name file site-lisp))
  1226.            (not (member file load-path)))
  1227.           (setq load-path
  1228.             (nconc load-path
  1229.                (list (file-name-as-directory file)))))
  1230.       (setq files (cdr files)))))
  1231.  
  1232.     ;; add lisp dir to load-path
  1233.     (when lisp
  1234.       ;; If the lisp dir isn't on the load-path, add it to the end.
  1235.       (or (member lisp load-path)
  1236.       (setq load-path (append load-path (list (concat lisp "/")))))
  1237.       ;; Also add any direct subdirectories of the lisp directory
  1238.       ;; to the load-path.  But don't add dirs whose names begin
  1239.       ;; with dot or hyphen.
  1240.       (let ((files (directory-files lisp nil "^[^-.]" nil 'dirs-only))
  1241.         file)
  1242.     (while files
  1243.       (setq file (car files))
  1244.       (when (and (not (member file '("RCS" "CVS" "SCCS")))
  1245.              (setq file (expand-file-name file lisp))
  1246.              (not (member file load-path)))
  1247.         (setq load-path
  1248.           (nconc load-path
  1249.              (list (file-name-as-directory file)))))
  1250.       (setq files (cdr files)))))
  1251.  
  1252.     ;; 1996/12/6 by MORIOKA Tomohiko <morioka@jaist.ac.jp>
  1253.     ;;    define `default-load-path' for file-detect.el
  1254.     (setq default-load-path
  1255.       (append default-load-path
  1256.           (if site-lisp
  1257.               (list site-lisp))
  1258.           (if lisp
  1259.               (list lisp)
  1260.             )
  1261.           ))
  1262.  
  1263.     ;; 1997/03/06 by Jeff Miller <jmiller@bayserve.net>
  1264.     ;; initialize 'site-directory'.  This is the site-lisp dir used by 
  1265.     ;; XEmacs
  1266.     (if site-lisp
  1267.     (setq site-directory (file-name-as-directory site-lisp))
  1268.       )
  1269.     ;; If running from the build directory, always prefer the exec-directory
  1270.     ;; that is here over to the one that came from paths.h.
  1271.     (when (or (and (null exec-directory) lib-src)
  1272.           (and (string= lib-src (expand-file-name "lib-src" root))
  1273.            (not (string= exec-directory lib-src))))
  1274.       (setq exec-directory (file-name-as-directory lib-src)))
  1275.     (when (or (and (null doc-directory) lib-src)
  1276.           (and (string= lib-src (expand-file-name "lib-src" root))
  1277.            (not (string= doc-directory lib-src))))
  1278.       (setq doc-directory (file-name-as-directory lib-src)))
  1279.  
  1280.     (when exec-directory
  1281.       (or (member exec-directory exec-path)
  1282.       (setq exec-path (append exec-path (list exec-directory)))))
  1283.     (when (or (and (null data-directory) etc)
  1284.           (and (string= etc (expand-file-name "etc" root))
  1285.            (not (string= data-directory etc))))
  1286.       (setq data-directory (file-name-as-directory etc)))
  1287.  
  1288.     ;; If `configure' specified an info dir, use it.
  1289.     ;; #### The above comment is suspect.
  1290.     (or (boundp 'Info-default-directory-list)
  1291.     (setq Info-default-directory-list nil))
  1292.  
  1293.     ;; Add additional system directories.
  1294.     (setq Info-default-directory-list
  1295.       (append Info-default-directory-list
  1296.           (split-string infopath-internal ":")))
  1297.  
  1298.     (let ((infopath (getenv "INFOPATH")))
  1299.       (when infopath
  1300.     (setq Info-default-directory-list
  1301.           (append Info-default-directory-list
  1302.               (split-string infopath ":")))))
  1303.  
  1304.     (cond (configure-info-directory
  1305.        (setq configure-info-directory (file-name-as-directory
  1306.                        configure-info-directory))
  1307.        (or (member configure-info-directory Info-default-directory-list)
  1308.            (setq Info-default-directory-list
  1309.              (append (list configure-info-directory)
  1310.                  Info-default-directory-list)))))
  1311.     ;; If we've guessed the info dir, use that (too).
  1312.     (when (and info (not (member info Info-default-directory-list)))
  1313.       (setq Info-default-directory-list
  1314.         (append (list info) Info-default-directory-list)))
  1315.  
  1316.     ;; Default the lock dir to being a sibling of the data-directory.
  1317.     ;; If superlock isn't set, or is set to a file in a nonexistent
  1318.     ;; directory, derive it from the lock dir.
  1319.     (when (boundp 'lock-directory)
  1320.       (setq lock-directory lock)
  1321.       (cond ((null lock-directory)
  1322.          (setq superlock-file nil))
  1323.         ((or (null superlock-file)
  1324.          (not (file-directory-p
  1325.                (file-name-directory superlock-file))))
  1326.          (setq superlock-file
  1327.            (expand-file-name "!!!SuperLock!!!"
  1328.                      lock-directory)))))
  1329.  
  1330.     (set-default-load-path-warning)
  1331.     (when (and data-directory Info-default-directory-list)
  1332.       (setq data-directory-list (list data-directory))
  1333.       (packages-find-packages package-path inhibit-package-init))))
  1334.  
  1335.  
  1336. (defun set-default-load-path-warning ()
  1337.   (let ((lock (if (boundp 'lock-directory) lock-directory 't))
  1338.     warnings message guess)
  1339.     (when (and (stringp lock) (not (file-directory-p lock)))
  1340.       (setq lock nil))
  1341.     (cond
  1342.      ((not (and exec-directory data-directory doc-directory load-path lock))
  1343.       (save-excursion
  1344.     (set-buffer (get-buffer-create " *warning-tmp*"))
  1345.     (erase-buffer)
  1346.     (buffer-disable-undo (current-buffer))
  1347.     (when (null lock)           (push "lock-directory" warnings))
  1348.     (when (null exec-directory) (push "exec-directory" warnings))
  1349.     (when (null data-directory) (push "data-directory" warnings))
  1350.     (when (null doc-directory)  (push "doc-directory"  warnings))
  1351.     (when (null load-path)      (push "load-path"      warnings))
  1352.     (cond ((cdr (cdr warnings))
  1353.            (setq message (apply 'format "%s, %s, and %s" warnings)))
  1354.           ((cdr warnings)
  1355.            (setq message (apply 'format "%s and %s" warnings)))
  1356.           (t (setq message (format "variable %s" (car warnings)))))
  1357.     (insert "couldn't find an obvious default for " message
  1358.         ", and there were no defaults specified in paths.h when "
  1359.         "XEmacs was built.  Perhaps some directories don't exist, "
  1360.         "or the XEmacs executable, " (concat invocation-directory
  1361.                              invocation-name)
  1362.         " is in a strange place?")
  1363.     (setq guess (or exec-directory
  1364.             data-directory
  1365.             doc-directory
  1366.             (car load-path)
  1367.             (and (string-match "/[^/]+\\'" invocation-directory)
  1368.                  (substring invocation-directory 0
  1369.                     (match-beginning 0)))))
  1370.     (when (and guess
  1371.            (or
  1372.             ;; parent of a terminal bin/<configuration> pair (hack hack).
  1373.             (string-match (concat "/bin/"
  1374.                       (regexp-quote system-configuration)
  1375.                       "/?\\'")
  1376.                   guess)
  1377.             ;; parent of terminal src, lib-src, etc, or lisp dir.
  1378.             (string-match
  1379.              "/\\(bin\\|src\\|lib-src\\|etc\\|lisp\\)[^/]*/?\\'"
  1380.              guess)))
  1381.       (setq guess (substring guess 0 (match-beginning 0))))
  1382.  
  1383.     ;; If neither the exec nor lisp dirs are around, then "guess" that
  1384.     ;; the new configure-style lib dir should be used.  Otherwise, if
  1385.     ;; only one of them appears to be missing, or it's just lock,
  1386.     ;; then guess it to be a sibling of whatever already exists.
  1387.     (when (and (null exec-directory) (null load-path))
  1388.       (setq guess (expand-file-name (startup-make-version-dir) guess)))
  1389.  
  1390.     (when (or (null exec-directory) (null load-path))
  1391.       (insert
  1392.        "\n\nWithout both exec-directory and load-path, XEmacs will "
  1393.        "be very broken.  "))
  1394.     (when (and (null exec-directory) guess)
  1395.       (insert
  1396.        "Consider making a symbolic link from "
  1397.        (expand-file-name system-configuration guess)
  1398.        " to wherever the appropriate XEmacs exec-directory "
  1399.        "directory is"))
  1400.     (when (and (null data-directory) guess)
  1401.       (insert
  1402.        (if exec-directory
  1403.            "\n\nConsider making a symbolic link " ", and ")
  1404.        "from "
  1405.        (expand-file-name "etc" (if load-path
  1406.                        (file-name-directory
  1407.                     (directory-file-name
  1408.                      (car load-path)))
  1409.                      guess))
  1410.        " to wherever the appropriate XEmacs data-directory is"))
  1411.     (when (and (null load-path) guess)
  1412.       (insert
  1413.        (if (and exec-directory data-directory)
  1414.            "Consider making a symbolic link "
  1415.          ", and ")
  1416.        "from "
  1417.        (expand-file-name "lisp" guess)
  1418.        " to wherever the appropriate XEmacs lisp library is"))
  1419.     (insert ".")
  1420.  
  1421.     (when (null lock)
  1422.       (insert
  1423.        "\n\nWithout lock-directory set, file locking won't work.  ")
  1424.       (when guess
  1425.         (insert
  1426.          "Consider creating "
  1427.          (expand-file-name "../xemacs/lock"
  1428.                    (or (find-emacs-root-internal
  1429.                     (concat invocation-directory
  1430.                         invocation-name))
  1431.                    guess))
  1432.          " as a directory or symbolic link for use as the lock "
  1433.          "directory.  (This directory must be globally writable.)"
  1434.          )))
  1435.  
  1436.     (when (fboundp 'fill-region)
  1437.       ;; Might not be bound in the cold load environment...
  1438.       (let ((fill-column 76))
  1439.         (fill-region (point-min) (point-max))))
  1440.     (goto-char (point-min))
  1441.     (princ "\nWARNING:\n" 'external-debugging-output)
  1442.     (princ (buffer-string) 'external-debugging-output)
  1443.     (erase-buffer)
  1444.     t)))))
  1445.  
  1446. ;;; startup.el ends here
  1447.